import Vue from 'vue';
import VueRouter from "vue-router";
import store from "./store";

import Layout from './layouts/Layout';
import Home from './pages/Home';
import Cafes from "./pages/Cafes";
import NewCafe from "./pages/NewCafe";
import Cafe from "./pages/Cafe";

import Login from "./pages/Login";
import Register from "./pages/Register";

import UserProfile from "./pages/UserProfile";
import City from './pages/city';
import el from "element-ui/src/locale/lang/el";

Vue.use(VueRouter);

function requireAuth(to,from,next) {
    function proceed() {
        //判断用户是否登录
        var localToken = window.localStorage.getItem('token');
        //var tokenExpiredIn = window.localStorage.getItem('tokenExpiredIn');
        if(localToken){
            // var nowTime = this.myCommon.getNowTime();
            // if(nowTime<= tokenExpiredIn){
            //     next();
            // }else {
            //     next('/login');
            // }
            next();
        }else{
            next('/login');
        }
        // if(store.getters.getUserLoadStatus() ===2){
        //     var user = store.getters.getUser;
        //     if(user.id){
        //         next();
        //     }else {
        //         next('/login')
        //     }
        // }else {
        //     next('/login')
        // }
    }
    // if(store.getters.getUserLoadStatus()!==2){
    //     store.dispatch('loadUser');
    //
    //     store.watch(store.getters.getUserLoadStatus,function () {
    //         if(store.getters.getUserLoadStatus() === 2){
    //             proceed();
    //         }
    //     });
    //     proceed();
    // }else {
    //     proceed();
    // }
    proceed();
}


export default new VueRouter({
    routes:[
        {
            path:'/login',
            name:'login',
            component:Vue.component('login',Login),
        },
        {
            path:'/register',
            name:'register',
            component:Vue.component('register',Register),
        },
        {
            path:'/',
            name:'layout',
            //component:Vue.component('Layout',Layout),
            component:Vue.component('Home',Layout),
            redirect:{name:'cafes'},
            children:[
                // {
                //     path:'home',
                //     name:'home',
                //     component:Vue.component('Home',Home)
                //     //component:Home
                // },
                {
                    path:'cafes',
                    name:'cafes',
                    //component:Vue.component('Cafes',Cafes),
                    component:Vue.component('Home',Home),
                    children:[
                        {
                            path:'new',
                            name:'newcafe',
                            component:Vue.component('NewCafe',NewCafe),
                            beforeEnter:requireAuth,
                        },
                        {
                            path:':id',
                            name:'cafe',
                            component:Vue.component('Cafe',Cafe)
                        },
                        {
                            path:'cities/:slug',
                            name:'city',
                            component:Vue.component('City',City)
                        },
                    ]
                },
                {
                    path:'profile',
                    name:'profile',
                    component:Vue.component('Profile',UserProfile),
                    beforeEnter:requireAuth,
                },
                {
                    path: '_=_',
                    redirect: '/'
                }
            ]
        },

    ]
})
